/**
 * \file Token.cpp
 *
 * Asteriskos Compiler 1
 * Written 2010-2011, by Anthony Berlin and Alexander Carlqvist
 *
 * This file is part of ICC.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "Token.h"

///
/// Constructors for \c Token.
///
Token::Token(SourceLine* line, TokenId type, string lexeme)
{
    init();

    if (line)
        m_line = line;

    m_type = type;
    m_lexeme = lexeme;
}

Token::Token()
{
    init();
}

///
/// Init some default values for data members.
///
inline void Token::init()
{
    m_type = (TokenId)0;
    m_lexeme = "";
    m_line = NULL;

    isLiteral = false;
    isPunctuator = false;
    isIdentifier = false;
    isKeyword = false;
    isSpecifier = false;
}

///
/// Poke a character onto the lexeme.
///
void Token::poke(char character)
{
    m_lexeme += character;
}

///
/// Return a string-representation of the token type.
///
string Token::convert_type_to_string() const
{
    string result;

    switch (m_type)
    {
        case TOK_NONE:
            result = "none";
            break;
        case TOK_EOF:
            result = "end-of-file";
            break;
        case TOK_INTEGER:
            result = "integer";
            break;
        case TOK_BOOLEAN:
            result = "boolean";
            break;
        case TOK_IDENT:
            result = "ident";
            break;
        case TOK_OPEN_BRACE:
        case TOK_CLOSE_BRACE:
        case TOK_OPEN_PAREN:
        case TOK_CLOSE_PAREN:
        case TOK_SEMICOLON:
        case TOK_PLUS:
        case TOK_MINUS:
        case TOK_ASTERISK:
        case TOK_SLASH:
        case TOK_ASSIGN:
        case TOK_COMMA:
        case TOK_OR:
        case TOK_AND:
        case TOK_GREATER:
        case TOK_GEQ:
        case TOK_LESS:
        case TOK_LEQ:
        case TOK_NOT:
        case TOK_EQUAL:
        case TOK_NOT_EQUAL:
            result = "punct";
            break;
        case RID_STATIC:
        case RID_CONST:
        case RID_EXTERN:
        case RID_SIGNED:
        case RID_UNSIGNED:
            result = "specf";
            break;
        case RID_INT:
        case RID_BOOL:
            result = "simple-decl";
            break;
        default:
            result = "unknown";
    }

    return result;
}

///
/// Return a string-representation of the token.
///
string Token::to_string() const
{
    return (string)"<" + convert_type_to_string() + " : " + m_lexeme + ">";
}
